package com.jye.cloud.user.service.impl;


import com.jye.cloud.domain.User;
import com.jye.cloud.user.mapper.UserMapper;
import com.jye.cloud.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public void userLoginById(int id) {
        Date date = new Date();
        User user = new User();
        user.setId(id);
        user.setLastLoginTime(date);
        userMapper.updateUser(user);
    }

    @Override
    public int updateUser(User user) {
        try {
            userMapper.updateUser(user);
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public List<Date> findUserLoginHistoryById(int id) {
        List<Date> dateList = userMapper.findUserLoginHistoryById(id);
        return dateList;
    }

    @Override
    public User login(String username, String password) {
        User user;
        try {
            user = userMapper.login(username, password);
            userMapper.updateLoginRecord(user.getId(), new Date());
            return user;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public int addUser(User user) {
        try {
            userMapper.addUser(user);
            return 1;
        } catch (Exception e) {
            System.out.println(e);
            return 0;

        }
    }

    @Override
    public Boolean isExitUsername(String username) {
        int i = userMapper.isExitUsername(username);
        if (i == 0) {
            return true;
        }
        return false;
    }
}
